
import * as Cesium from 'cesium'

import PointPrimitiveManage from "@/utils/primitiveCollectionManage/PointPrimitiveManage";
import LabelPrimitiveManage from "@/utils/primitiveCollectionManage/LabelPrimitiveManage";
import BillboardPrimitiveManage from "@/utils/primitiveCollectionManage/BillboardPrimitiveManage";

class PrimitiveGraphic{

  constructor(options) {

    this.options = options;
    this.primitiveCollection = new Cesium.PrimitiveCollection()

    this._pointCollection = null;
    this._labelCollection = null;
    this._billboardCollection = null;

    this._collection = new Map();
  }

  get pointCollection(){
    if(!this._pointCollection){
      this._pointCollection = new PointPrimitiveManage(Cesium.defaultValue(this.options,null));
      this.primitiveCollection.add(this._pointCollection);
    }
    return this._pointCollection
  }

  get labelCollection(){
    if(!this._labelCollection){
      this._labelCollection = new LabelPrimitiveManage(Cesium.defaultValue(this.options,null));
      this.primitiveCollection.add(this._labelCollection);
    }
    return this._labelCollection
  }

  get billboardCollection(){
    if(!this._billboardCollection){
      this._billboardCollection = new BillboardPrimitiveManage(Cesium.defaultValue(this.options,null));
      this.primitiveCollection.add(this._billboardCollection)
    }
    return this._billboardCollection;
  }

  add(options){

    if(!Cesium.defined(options)){
      console.error('options不能为空！')
      return false;
    }

    const {point,label,billboard,id,position} = options;

    const renderId = Cesium.defined(id) ? id : Cesium.createGuid()

    let renderPoint,renderLabel,renderBillboard;

    if(point){
      renderPoint = this.pointCollection.add({
        ...point,
        id:renderId,
        position
      })
    }
    if(label){
      renderLabel = this.labelCollection.add({
        ...label,
        id:renderId,
        position
      })
    }
    if(billboard){
      renderBillboard = this.billboardCollection.add({
        ...billboard,
        id:renderId,
        position
      })
    }

    const c = {
      point:renderPoint,
      label:renderLabel,
      billboard:renderBillboard,
      id:renderId,
      get position(){
        return position
      },
      set position(val){
        if(renderPoint) renderPoint.position = val;
        if(renderLabel) renderLabel.position = val;
        if(renderBillboard) renderBillboard.position = val
      }
      // position:position,
    }
    this._collection.set(renderId,c)
    return c;
  }

  getById(id){
    return this._collection.get(id)
  }

}

export default PrimitiveGraphic;